package com.yj.suncity.activity.shouye;

import java.util.HashMap;

import org.json.JSONObject;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;

import com.yj.suncity.R;
import com.yj.suncity.async.AsyncTaskUtil;
import com.yj.suncity.async.AsyncTaskUtil.ResultCallBack;
import com.yj.suncity.constant.ShareCon;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.onekeyshare.ShowShare;
import com.yj.suncity.utis.SharedPreferencesUtil;
import com.yj.suncity.utis.ToastUtil;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class SigninActivity extends Activity {
	private Context context;
	private String userId;
	private int mWidth;
	private ImageView mImg_sign_head;
	private SharedPreferencesUtil preferencesUtil = SharedPreferencesUtil
			.getInstance();
	private SoundPool pool;
	private boolean isSound;
	private ImageView mImageView_anim;
	private AnimationDrawable drawable;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_signin);
		context = this;
		pool = new SoundPool(2, AudioManager.STREAM_SYSTEM, 5);
		pool.load(this, R.raw.bgm_coin_01, 1);
		mWidth = getResources().getDisplayMetrics().widthPixels;
		userId = getIntent().getStringExtra("userid");
		mImg_sign_head = (ImageView) findViewById(R.id.img_sign_head);
		mImg_sign_head.setLayoutParams(new RelativeLayout.LayoutParams(mWidth,
				mWidth / 2));
		mImageView_anim = (ImageView) findViewById(R.id.img_anmiation);
		mImageView_anim.setVisibility(View.INVISIBLE);
		drawable = (AnimationDrawable) mImageView_anim.getBackground();
		isSound = preferencesUtil.getBoolean(context, ShareCon.USERFILE,
				ShareCon.SOUND, true);

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnHeadBack:
			this.finish();
			break;
		case R.id.btn_sign_sign:
			// signSign();
			break;
		case R.id.btn_sign_share:
			signShare();
			break;
		default:
			break;
		}
	}

	private void signSign() {
		String path = URLCon.用户 + "?action=signin&memberid=" + userId
				+ "&goldnum=2";
		Log.d("", path);
		AsyncTaskUtil asyncTaskUtil = new AsyncTaskUtil(context, null, null,
				false, null, true, "签到......", new ResultCallBack() {
					@Override
					public void JSONResult(JSONObject jsonObject) {
						if (jsonObject.optString("success").equals("1")) {
							if (isSound) {
								pool.play(1, 1, 1, 0, 0, 1);
							} else {
								ToastUtil.ShowToast(context, "签到成功！");
							}
							mImageView_anim.setVisibility(View.VISIBLE);
							drawable.start();
							Thread thread = new Thread(new Runnable() {
								@Override
								public void run() {
									try {
										Thread.sleep(3000);
										SigninActivity.this.finish();
									} catch (InterruptedException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								}
							});
							thread.start();
						} else {
							ToastUtil.ShowToast(context,
									jsonObject.optString("msg"));
						}
					}
				});
		asyncTaskUtil.execute(path);
	}

	private void signShare() {
		ShowShare share =new ShowShare(this.context, "WechatMoments", new OneKeyShareCallback());
		share.showShare("大家都在用，分享也赚钱，你也赶快加入", "大家都在用，分享也赚钱，你也赶快加入", URLCon.HOST+"/web/index.html", "");
	}

	public class OneKeyShareCallback implements PlatformActionListener {

		public void onComplete(Platform plat, int action,
				HashMap<String, Object> res) {
			Log.d(getClass().getSimpleName(), res.toString());
			// 在这里添加分享成功的处理代码
			if (res.toString() != null && !res.toString().equals("")) {

				signSign();
			}
		}

		public void onError(Platform plat, int action, Throwable t) {
			t.printStackTrace();
			// 在这里添加分享失败的处理代码
			ToastUtil.ShowToast(context, "分享失败！");
		}

		public void onCancel(Platform plat, int action) {
			// 在这里添加取消分享的处理代码
			ToastUtil.ShowToast(context, "您已取消分享！");
		}

	}

}
